﻿using System.Linq.Expressions;
using System.Text;
using OS.Orm.Enums;

namespace OS.Orm.Core
{
    /// <summary>
    /// 表达式解析标识内容体
    /// </summary>
    public class SqlVistorFlag
    {
        /// <summary>
        /// 多行sql语句文本
        /// </summary>
        private readonly StringBuilder _sqlBuilder;

        /// <summary>
        /// 是否是表达式右侧项，决定右侧树是否做参数化处理
        /// </summary>
        public bool IsRight { get; set; }

        /// <summary>
        /// 主要是MethodCall方法中解析时需要知晓上层一元运算符
        /// </summary>
        public ExpressionType UnaryType { get; set; }

        /// <summary>
        /// 参数辨识符号
        /// </summary>
        public string ParameterPreToken { get; set; } = "@";

        /// <summary>
        /// 对应的Sql语句
        /// </summary>
        public string Sql => _sqlBuilder.ToString();

        /// <summary>
        /// 解析语段类型
        /// </summary>
        public SqlVistorType SqlVistorType { get; set; }

        #region 语句拼接

        private bool _isStarted;

        /// <summary>
        /// 往语句中追加内容
        /// </summary>
        /// <param name="content">内容</param>
        /// <param name="needCheckSep">是否检查步骤</param>
        /// <returns></returns>
        public SqlVistorFlag Append(string content, bool needCheckSep = false)
        {
            if (!_isStarted)
                _isStarted = true;
            else
            {
                if (needCheckSep)
                {
                    _sqlBuilder.Append(GetSeparate());
                }
            }

            _sqlBuilder.Append(content);
            return this;
        }

        /// <summary>
        /// 获取对应的参数名称，给参数添加前置符号
        /// </summary>
        /// <param name="name">参数名称</param>
        /// <returns></returns>
        public virtual string GetParameterName(string name)
        {
            return string.Concat(ParameterPreToken, name);
        }

        private int _constantParameterIndex;

        /// <summary>
        /// 获取定制常量参数名称，部分参数并无指定属性，这里自定义不重复参数
        /// </summary>
        /// <returns></returns>
        public virtual string GetCustomParameterName()
        {
            const string parameterPre = "ConstParameter";
            return string.Concat(ParameterPreToken, parameterPre, _constantParameterIndex++);
        }

        /// <summary>
        /// 获取属性列名称，内部补充转义字符，防止和数据库关键字名称冲突，如^name^
        /// </summary>
        /// <param name="name">属性列名称</param>
        /// <returns></returns>
        public virtual string GetColName(string name)
        {
            return string.Concat('`', name, '`');
        }

        /// <summary>
        /// 根据sql关键字进行检查
        /// </summary>
        /// <returns></returns>
        private string GetSeparate()
        {
            switch (SqlVistorType)
            {
                case SqlVistorType.Update:
                    return ",";
                case SqlVistorType.Where:
                    return " ";
                default:
                    return string.Empty;
            }
        }

        #endregion

        public SqlVistorFlag(SqlVistorType sqlVistorType)
        {
            _sqlBuilder = new StringBuilder();
            UnaryType = (ExpressionType)(-1);
        }
    }
}